﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using TopologySearch.LoopSearch;

namespace TopologySearch
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdTestGetSplitCurveWithOutsetFloor : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_uiApp = cmdData.Application;
			this.m_uiDoc = this.m_uiApp.ActiveUIDocument;
			this.m_app = this.m_uiApp.Application;
			this.m_doc = this.m_uiDoc.Document;
			IList<Reference> list = new List<Reference>();
			if (!this.SelectCurves(ref list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			new List<Pole2D>();
			List<Pole2D> list2 = new List<Pole2D>();
			double num = Common.MMToFeet(100.0);
			foreach (Reference reference in list)
			{
				Element element = this.m_doc.GetElement(reference);
				if (element.GetType() == typeof(Wall))
				{
					Pole2D item = Pole2D.TransformWallToStructure2DPole(element as Wall);
					list2.Add(item);
				}
				else if (element.GetType() == typeof(ModelLine))
				{
					Pole2D item2 = Pole2D.TransformWallToStructure2DPole((element as ModelLine).GeometryCurve, ElementId.InvalidElementId, num, num);
					list2.Add(item2);
				}
				else if (element.GetType() == typeof(ModelArc))
				{
					Pole2D item3 = Pole2D.TransformWallToStructure2DPole((element as ModelArc).GeometryCurve, ElementId.InvalidElementId, num, num);
					list2.Add(item3);
				}
				else if (element.GetType() == typeof(DetailLine))
				{
					Pole2D item4 = Pole2D.TransformWallToStructure2DPole((element as DetailLine).GeometryCurve, ElementId.InvalidElementId, 0.0, 0.0);
					list2.Add(item4);
				}
				else if (element.GetType() == typeof(DetailArc))
				{
					Pole2D item5 = Pole2D.TransformWallToStructure2DPole((element as DetailArc).GeometryCurve, ElementId.InvalidElementId, 0.0, 0.0);
					list2.Add(item5);
				}
			}
			List<List<Curve>> cellsEdgeLoops = new StructureLoopSearcher(cmdData).GetCellsEdgeLoops(list2, true);
			CurveArray curveArray = new CurveArray();
			foreach (List<Curve> list3 in cellsEdgeLoops)
			{
				foreach (Curve curve in list3)
				{
					curveArray.Append(curve);
				}
			}
			if (curveArray.Size > 0)
			{
				Transaction transaction = new Transaction(this.m_doc);
				transaction.Start("draw loop");
				this.m_doc.Create.NewDetailCurveArray(this.m_uiDoc.ActiveView, curveArray);
				transaction.Commit();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool SelectCurves(ref IList<Reference> selRefs)
		{
			bool result;
			try
			{
				selRefs = this.m_uiDoc.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new SearchDataFilter(), "请选择墙或参考线：");
				result = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		private UIApplication m_uiApp;

		private UIDocument m_uiDoc;

		private Application m_app;

		private Document m_doc;
	}
}
